package com.hngy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hngy.common.BaseContext;
import com.hngy.common.R;
import com.hngy.domain.OrderDetail;
import com.hngy.domain.Orders;
import com.hngy.domain.ShoppingCart;
import com.hngy.dto.OrdersDto;
import com.hngy.service.OrderDetailService;
import com.hngy.service.OrdersService;
import com.hngy.service.ShoppingCartService;
import com.sun.org.apache.xpath.internal.operations.Or;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author LiKe
 * @date 2021/12/29 18:53
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrdersController {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 后台分页查询
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R page(int page, int pageSize, String number, String beginTime, String endTime){
        log.info("beginTime = {},endTime = {}",beginTime,endTime);

        IPage<OrdersDto> iPage = ordersService.ordersPage(page, pageSize, number, beginTime, endTime);

        return R.success(iPage);
    }
    /**
     * 后台管理修改订单状态
     * @param orders
     * @return
     */
    @PutMapping
    public R status(@RequestBody Orders orders){
        log.info("orders:{}",orders);

        LambdaUpdateWrapper<Orders> updateWrapper = new LambdaUpdateWrapper<>();

        updateWrapper.set(Orders::getStatus,orders.getStatus());
        updateWrapper.eq(Orders::getId,orders.getId());

        ordersService.update(updateWrapper);

        return R.success("修改成功！");
    }

    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R submit(@RequestBody Orders orders){
        ordersService.saveOrders(orders);
        return R.success("下单成功！");
    }

    /**
     * 用户历史订单
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R userPage(int page,int pageSize){

        IPage<Orders> page1 = new Page<>(page,pageSize);

        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(Orders::getUserId, BaseContext.getId()).orderByDesc(Orders::getOrderTime);

        ordersService.page(page1,queryWrapper);

        return R.success(page1);
    }

    /**
     * 用户再来一单
     * @param orders
     * @return
     */
    @PostMapping("/again")
    public R again(@RequestBody Orders orders){
        log.info("orders:{}",orders);
        //根据订单id查询订单明细
        LambdaQueryWrapper<OrderDetail> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(OrderDetail::getOrderId,orders.getId());
        List<OrderDetail> orderDetails = orderDetailService.list(queryWrapper);

        log.info("orderslists:{}",orderDetails);
        //克隆对象
        List<ShoppingCart> list= orderDetails.stream().map((item)->{
            ShoppingCart shoppingCart = new ShoppingCart();

            BeanUtils.copyProperties(item,shoppingCart,"id");

            shoppingCart.setUserId(BaseContext.getId());
            shoppingCart.setCreateTime(LocalDateTime.now());
            return shoppingCart;

        }).collect(Collectors.toList());

        log.info("shoppingCart:{}",list);

        //添加购物车

        shoppingCartService.saveBatch(list);

        return R.success("添加成功！");
    }
}
